package com.yinghuo.risk.frigate.web.controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.yinghuo.risk.frigate.core.anno.HasPermission;
import com.yinghuo.risk.frigate.core.enums.Policy;
import com.yinghuo.risk.frigate.core.info.UserLoginEvent;
import com.yinghuo.risk.frigate.core.info.UserLoginInfo;
import com.yinghuo.risk.frigate.web.bean.base.ResultBean;
import com.yinghuo.risk.frigate.web.dao.PermissionRepository;
import com.yinghuo.risk.frigate.web.dao.RoleRepository;
import com.yinghuo.risk.frigate.web.dao.UserInfoRepository;
import com.yinghuo.risk.frigate.web.entity.UserInfoEntity;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Base64;

/**
 * com.yinghuo.risk.frigate.web.controller
 *
 * @author 白雨浓
 * date: 2018/10/16 21:12
 * content:
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

    private final UserInfoRepository userInfoRepository;

    private final PermissionRepository permissionRepository;

    private final RoleRepository roleRepository;

    private final UserLoginEvent userLoginEvent;

    @PostMapping("/login")
    public Object login() {
        UserInfoEntity entity = new UserInfoEntity();

        /*info.setId(1);
        info.setUserId("afafd");
        info.setPermissionList(Arrays.asList("test:get", "test:add"));
        info.setSource(new UserInfoEntity());


        String token = SecureUtil.md5(JSON.toJSONString(info));
        info.setToken(token);

        userLoginEvent.login(token, info);*/

        return ResultBean.success(null);
    }

    @PostMapping("/logout")
    public Object logout() {
        userLoginEvent.logout("tokenId");
        return new ResultBean();
    }

    @GetMapping
    @HasPermission(value = {"test:get", "test:modify"}, policy = Policy.OR)
    public Object getTest() {
        ResultBean bean = new ResultBean();
        bean.setData(permissionRepository.findAll());
        return bean;
    }

}
